import os,json
from conf import setting

#1.select
def select(name,addr=setting.USER_PATH):
    dir_path = os.path.join(addr)
    path = os.path.join(dir_path, '%s.json' %name)
    if os.path.exists(path):
        with open(path,'r',encoding='utf-8') as f:
            return json.load(f) #返回一个字典给调用者

    else:
        return False


#2.save
def save(user_dic,addr=setting.USER_PATH):
    dir_path=os.path.join(addr)
    path=os.path.join(dir_path,'%s.json'%user_dic['name'])

    with open(path,'w',encoding='utf-8') as f:
        json.dump(user_dic,f)
        f.flush()


#3.delete
def delete(name,addr=setting.GOODS_PATH):
    dir_path = os.path.join(addr)
    path = os.path.join(dir_path, '%s.json' %name)

    if os.path.exists(path):
        os.remove(path)
        return True
    else:
        return False

